package com.northpool.stpclient.demo;

import com.google.common.base.CaseFormat;
import com.northpool.resources.Constants;
import com.northpool.resources.datasource.IDataSource;
import com.northpool.resources.datasource.db.DbDataSource;
import com.northpool.resources.datasource.ogr.ShapeDataSource;
import com.northpool.resources.datatable.db.operate.column.PostgresColumn;
import com.northpool.resources.datatable.operate.ColumnBean;
import com.northpool.resources.dialect.IDialect;
import com.northpool.resources.dialect.sql.ISQLDialect;
import com.northpool.stpclient.StpClient;
import com.northpool.stpclient.bean.BaseResponse;
import com.northpool.stpclient.bean.datatransfer.HandlerClassParams;
import com.northpool.stpclient.bean.datatransfer.TableCopyBuildPlan;
import com.northpool.stpclient.bean.datatransfer.TbaleCopyModeEnum;
import com.northpool.stpclient.modules.geoprocess.datatransfer.IDataTransferOperator;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

public class TableCopyDemo {

    // stpserver访问地址
    public static final String host = "http://127.0.0.1:8088/stpserver";
    // 构建客户端
    public static StpClient client = new StpClient(host);

    // 创建数据源
    public static IDataSource createPGDataSource_kjklz() {
        String url = "127.0.0.1:5434/storage";
        com.northpool.resources.Constants.DATA_SOURCE_TYPE type = com.northpool.resources.Constants.DATA_SOURCE_TYPE.postgreSQL;
        String user = "postgres";
        String password = "postgres";
        return new DbDataSource(url, user, password, type);
    }

    public static IDataSource createShapeDataSource() {
        String path = "D:\\ebackup\\files\\testdata\\aisport\\port.shp";
        return new ShapeDataSource(path);
    }

    // 构建参数
    public static TableCopyBuildPlan getBuildPlan() {
        List<HandlerClassParams> middleHandlers = new LinkedList<>();
        HandlerClassParams params = HandlerClassParams.Builder.create()
                // 中间算子类
                .addClassName(GeometryFilterTransform.class)
                // 中间算子类构造函数参数类型
                .addTypeList(null)
                // 中间算子构造函数参数值
                .addParameters(null)
                .build();

        middleHandlers.add(params);

        // 传输过程中没有表时新增字段
        List<ColumnBean> columnlist = new LinkedList<>();
        PostgresColumn columnBean = new PostgresColumn("f_new", 100, 0, "varchar",
                true, null, null, (ISQLDialect) IDialect.getByType(Constants.DATA_SOURCE_TYPE.postgreSQL));
        columnlist.add(columnBean);

        // 1.class实例
        return TableCopyBuildPlan.TableCopyParamsBuilder.create()
                //来源数据库
                .addOriginDatasource(createShapeDataSource())
                //来源表名
                .addOriginTableName("port")
                //中间算子
                .addMiddleHandlers(middleHandlers)
                //目标数据源
                .addTargetDatasource(createPGDataSource_kjklz())
                //目标表名
                .addTargetTableName("public.port123")
                //合并，按主键不能冲突，如主键用重复，使用TbaleCopyModeEnum.APPENDER， 会建立新的自增主键
                .addMode(TbaleCopyModeEnum.MERGE)
                .addSpatialFieldNullAble(true)
                //新增字段
                .addAdditionalFields(columnlist)
                .addCaseFormat(CaseFormat.LOWER_CAMEL)
                .build();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        // 1.构建参数
        TableCopyBuildPlan buildPlan = getBuildPlan();
        System.out.println(buildPlan.toJson());

        // 2.获取数据传输操作实例
        IDataTransferOperator dataTransferOperator = client.dataTransferOperator();
        // 3.开始表拷贝
        BaseResponse result = dataTransferOperator.startTableCopy(buildPlan);
        // 执行成功回调
        dataTransferOperator.onSuccess(result.getRequestId(), () -> {
            System.out.println("success");
            System.exit(0);
        });
        // 执行错误回调
        dataTransferOperator.onError(result.getRequestId(), () -> {
            System.out.println("failed");
            String log = dataTransferOperator.log(result.getRequestId());
            System.out.println(log);
            System.exit(1);
        });
    }

}